package com.ssy.lingxi.member.merchant.utils.baidu;

/**
 * aesKey工具类
 * @author lqa
 * @version 2.0.0
 * @date 2022/1/11
 */
public class AesKeyUtil {
    private static final String HEX = "0123456789abcdef";

    /**
     * 获得原生的128位的aeskey
     * 因为一个byte位8位最后生成的byte数组长度为16
     * <p>
     * 16 * 8 = 128
     *
     */
    public static byte[] parseAesKey(String hex) throws Exception {
        char[] data = hex.toCharArray();
        if (data.length != 16) {
            throw new Exception(" ase key illegal ");
        }
        return decode(hex.toCharArray());
    }

    private static byte[] decode(char[] data) throws IllegalArgumentException {
        int len = data.length;

        byte[] out = new byte[len];

        for (int i = 0; i < len; i++) {
            int f = toDigit(data[i]);
            out[i] = (byte) (f);
        }
        return out;
    }

    private static int toDigit(char ch) {
        return HEX.indexOf(ch);
    }
}
